{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from collections import deque, defaultdict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def stable_match(men, women):\n",
    "    free_men = deque(men)\n",
    "    engaged = defaultdict(lambda: None)\n",
    "\n",
    "    while free_men:\n",
    "        i = free_men.popleft()\n",
    "\n",
    "        # man proposes women according his preferences\n",
    "        for j in men[i]:\n",
    "            preference = women[j].index\n",
    "            fiance = engaged[j]\n",
    "\n",
    "            # woman accepts the better offer\n",
    "            if not fiance or preference(i) < preference(fiance):\n",
    "                engaged[j] = i\n",
    "                fiance and free_men.append(fiance)\n",
    "                break\n",
    "\n",
    "    return [(m, w) for w, m in engaged.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "men = {\n",
    "    'adam': ['claire', 'diana'],\n",
    "    'bob': ['diana', 'claire'],\n",
    "}\n",
    "women = {\n",
    "    'claire': ['bob', 'adam'],\n",
    "    'diana': ['adam', 'bob'],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('adam', 'claire'), ('bob', 'diana')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stable_match(men, women)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "men = {\n",
    "    'adam': ['betty', 'claire', 'diana'],\n",
    "    'bob': ['betty', 'claire', 'diana'],\n",
    "    'charlie': ['betty', 'claire', 'diana'],\n",
    "}\n",
    "women = {\n",
    "    'betty': ['charlie', 'bob', 'adam'],\n",
    "    'claire': ['charlie', 'bob', 'adam'],\n",
    "    'diana': ['charlie', 'bob', 'adam'],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('charlie', 'betty'), ('bob', 'claire'), ('adam', 'diana')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stable_match(men, women)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "men = {\n",
    "    'adam': ['diana', 'alice', 'betty', 'claire'],\n",
    "    'bob': ['betty', 'claire', 'alice', 'diana'],\n",
    "    'charlie': ['betty', 'diana', 'claire', 'alice'],\n",
    "    'david': ['claire', 'alice', 'diana', 'betty'],\n",
    "}\n",
    "women = {\n",
    "    'alice': ['david', 'adam', 'charlie', 'bob'],\n",
    "    'betty': ['adam', 'charlie', 'bob', 'david'],\n",
    "    'claire': ['adam', 'bob', 'charlie', 'david'],\n",
    "    'diana': ['david', 'adam', 'charlie', 'bob'],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('adam', 'diana'),\n",
       " ('charlie', 'betty'),\n",
       " ('bob', 'claire'),\n",
       " ('david', 'alice')]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stable_match(men, women)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
